import java.util.Arrays;

public class MyStack2<E> {

    public Object[] elem;
    public int usedSize;

    public MyStack2(){
        this.elem = new Object[10];
    }

    public void push(E val){
        if(elem.length == usedSize){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }

    public E pop(){
        try {
            checkEmpty();
        }catch (StackIsEmptyException e){
            e.printStackTrace();
        }
        E oldVal = (E)elem[usedSize - 1];
        usedSize--;
        return oldVal;
    }

    private void checkEmpty()throws StackIsEmptyException{
        if(usedSize == 0){
            throw new StackIsEmptyException("栈空间为空");
        }
    }

    public E peek(){
        if(empty()){
            return null;
        }
        E oldVal = (E)elem[usedSize - 1];
        return oldVal;
    }

    public boolean empty(){
        return elem.length == 0;
    }


}
